package com.example.drainage.ui.downwin

import com.example.arm.ext.dpInt
import com.example.drainage.base.ktexpand.visible
import com.example.drainage.databinding.ActivityDownWinBinding
import java.util.*

/**
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.example.armmvvm.ui.downwin
 * @createTime 创建时间 ：2021/12/9
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
sealed class DownWinType(val binding: ActivityDownWinBinding) {

    abstract fun show()

    open fun close() {
    }

    abstract fun ensure()

    abstract fun reset()
}

class DownWinTimeType(binding: ActivityDownWinBinding) : DownWinType(binding) {

    private val startYear = 1990

    private val curYear = Calendar.getInstance().get(Calendar.YEAR)
    private var selectYear = curYear

    private var curMonth = Calendar.getInstance().get(Calendar.MONTH) + 1
    private var selectMonth = curMonth

    private val yearList = mutableListOf<DownWinTextAdapter.DownWinTextBean>().apply {
        for (i in startYear..selectYear) {
            add(DownWinTextAdapter.DownWinTextBean("${i}年"))
        }
    }
    private val monthList = mutableListOf<DownWinTextAdapter.DownWinTextBean>().apply {
        for (i in 1..12) {
            add(DownWinTextAdapter.DownWinTextBean(if (i < 10) "0${i}月" else "${i}月"))
        }
    }
    private val curMonthList = mutableListOf<DownWinTextAdapter.DownWinTextBean>().apply {
        for (i in 1..curMonth) {
            add(DownWinTextAdapter.DownWinTextBean(if (i < 10) "0${i}月" else "${i}月"))
        }
    }

    private val yearAdapter = DownWinTextAdapter().apply {
        selectPosition = yearList.size - 1
        setNewInstance(yearList)
    }

    private val monthAdapter = DownWinTextAdapter(true).apply {
        selectPosition = curMonthList.size - 1
        setNewInstance(curMonthList)
    }

    init {
        binding.apply {
            tvTime.text = if (selectMonth < 10) "${selectYear}-0${selectMonth}" else "${selectYear}-${selectMonth}"
            tvTime.isSelected = true
            ivTime.isSelected = true
        }
        yearAdapter.onSelectListener = { _, downWinTextBean: DownWinTextAdapter.DownWinTextBean ->
            if (downWinTextBean.text.startsWith(curYear.toString())) {
                if (monthAdapter.selectPosition > curMonthList.size - 1) {
                    monthAdapter.selectPosition = 0
                }
                monthAdapter.setNewInstance(curMonthList)
            } else {
                monthAdapter.setNewInstance(monthList)
            }
        }
    }

    override fun show() {
        binding.apply {
            recyclerView2.visible = true
            recyclerView3.visible = false

            recyclerView1.adapter = yearAdapter
            recyclerView2.adapter = monthAdapter

            binding.recyclerView2.setPadding(0, 0, 10.dpInt, 0)

            scrollToRightPosition()
        }
    }

    override fun ensure() {
        selectYear = startYear + yearAdapter.selectPosition
        selectMonth = monthAdapter.selectPosition + 1
        binding.tvTime.text = if (selectMonth < 10) "${selectYear}-0${selectMonth}" else "${selectYear}-${selectMonth}"
    }

    override fun reset() {
        val tempSelectYear = curYear
        val tempSelectMonth = curMonth
        val selectPosition = tempSelectYear - startYear
        yearAdapter.selectPosition = selectPosition
        yearAdapter.onSelectListener?.invoke(selectPosition, DownWinTextAdapter.DownWinTextBean("${tempSelectYear}年"))
        monthAdapter.selectPosition = tempSelectMonth - 1

        yearAdapter.notifyDataSetChanged()
        monthAdapter.notifyDataSetChanged()
        scrollToRightPosition()
    }

    override fun close() {
        super.close()
        binding.recyclerView2.setPadding(0, 0, 0, 0)
        val selectPosition = selectYear - startYear
        yearAdapter.selectPosition = selectPosition
        yearAdapter.onSelectListener?.invoke(selectPosition, DownWinTextAdapter.DownWinTextBean("${selectYear}年"))
        monthAdapter.selectPosition = selectMonth - 1
    }

    //---------------------自定义方法---------------------------

    private fun scrollToRightPosition() {
        if (yearAdapter.selectPosition >= 0) {
            binding.recyclerView1.scrollToPosition(yearAdapter.selectPosition)
        }
        if (monthAdapter.selectPosition >= 0) {
            binding.recyclerView2.scrollToPosition(monthAdapter.selectPosition)
        }
    }
}

class DownWinDistanceType(binding: ActivityDownWinBinding) : DownWinType(binding) {

    private val distanceAdapter = DownWinTextAdapter(true).apply {
        selectPosition = 0
        setNewInstance(
            mutableListOf(
                DownWinTextAdapter.DownWinTextBean("不限"),
                DownWinTextAdapter.DownWinTextBean("500米"),
                DownWinTextAdapter.DownWinTextBean("1000米"),
                DownWinTextAdapter.DownWinTextBean("1500米"),
                DownWinTextAdapter.DownWinTextBean("2000米"),
                DownWinTextAdapter.DownWinTextBean("2500米"),
                DownWinTextAdapter.DownWinTextBean("3000米"),
            )
        )
    }

    private var defaultDistance = 0

    private var selectDistance = defaultDistance

    override fun show() {
        binding.apply {
            recyclerView2.visible = false
            recyclerView3.visible = false

            recyclerView1.setPadding(0, 0, 10.dpInt, 0)
            recyclerView1.adapter = distanceAdapter

            scrollToRightPosition()
        }
    }

    override fun ensure() {
        selectDistance = distanceAdapter.selectPosition
        binding.tvDistance.text = distanceAdapter.getItem(selectDistance).text
        binding.tvDistance.isSelected = true
        binding.ivDistance.isSelected = true
    }

    override fun reset() {
        val selectDistance = defaultDistance
        distanceAdapter.selectPosition = selectDistance
        distanceAdapter.notifyDataSetChanged()
    }

    override fun close() {
        super.close()
        binding.recyclerView1.setPadding(0, 0, 0, 0)
        distanceAdapter.selectPosition = selectDistance
    }

    //---------------------自定义方法---------------------------
    private fun scrollToRightPosition() {
        if (distanceAdapter.selectPosition >= 0) {
            binding.recyclerView1.scrollToPosition(distanceAdapter.selectPosition)
        }
    }
}

class DownWinDistrictType(binding: ActivityDownWinBinding) : DownWinType(binding) {

    private val districtAdapter = DownWinTextAdapter().apply {
        selectPosition = 0
        setNewInstance(
            mutableListOf(
                DownWinTextAdapter.DownWinTextBean("不限"),
                DownWinTextAdapter.DownWinTextBean("越秀区"),
                DownWinTextAdapter.DownWinTextBean("荔湾区"),
                DownWinTextAdapter.DownWinTextBean("海珠区"),
                DownWinTextAdapter.DownWinTextBean("天河区"),
                DownWinTextAdapter.DownWinTextBean("白云区"),
                DownWinTextAdapter.DownWinTextBean("黄埔区"),
                DownWinTextAdapter.DownWinTextBean("番禺区"),
                DownWinTextAdapter.DownWinTextBean("花都区"),
                DownWinTextAdapter.DownWinTextBean("南沙区"),
                DownWinTextAdapter.DownWinTextBean("增城区"),
                DownWinTextAdapter.DownWinTextBean("从化区"),
            )
        )
    }

    private val pieceAdapter = DownWinTextAdapter().apply {
        selectPosition = 0
        setNewInstance(
            mutableListOf(
                DownWinTextAdapter.DownWinTextBean("不限"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD1"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD2"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD3"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD4"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD5"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD6"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD7"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD8"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD9"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD11"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD11"),
            )
        )
    }

    private val placeAdapter = DownWinTextAdapter(true).apply {
        selectPosition = 0
        setNewInstance(
            mutableListOf(
                DownWinTextAdapter.DownWinTextBean("不限"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD1"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD2"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD3"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD4"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD5"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD6"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD7"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD8"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD9"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD11"),
                DownWinTextAdapter.DownWinTextBean("A-B-CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD11"),
            )
        )
    }

    private var defaultDistrictPosition = 0

    private var selectDistrictPosition = 0

    private var selectPiecePosition = 0

    private var selectPlacePosition = 0

    init {
        binding.apply {
            districtAdapter.onSelectListener = { position: Int, downWinTextBean: DownWinTextAdapter.DownWinTextBean ->
                if (position == 0) {
                    recyclerView2.visible = false
                    recyclerView3.visible = false
                } else {
                    recyclerView2.visible = true
                    recyclerView3.visible = false
//                pieceAdapter.setNewInstance()
                    pieceAdapter.notifyDataSetChanged()
                }
                pieceAdapter.selectPosition = 0
                placeAdapter.selectPosition = 0
                scrollToRightPosition()
            }
            pieceAdapter.onSelectListener = { position: Int, downWinTextBean: DownWinTextAdapter.DownWinTextBean ->
                if (position == 0) {
                    recyclerView3.visible = false
                } else {
                    recyclerView3.visible = true
//                pieceAdapter.setNewInstance()
                    placeAdapter.notifyDataSetChanged()
                }
                placeAdapter.selectPosition = 0
                scrollToRightPosition()
            }
        }
    }

    override fun show() {
        binding.apply {
            recyclerView2.visible = selectDistrictPosition != 0
            recyclerView3.visible = selectDistrictPosition != 0 && selectPiecePosition != 0
            recyclerView3.setPadding(0, 0, 10.dpInt, 0)

            recyclerView1.adapter = districtAdapter
            recyclerView2.adapter = pieceAdapter
            recyclerView3.adapter = placeAdapter
            scrollToRightPosition()
        }
    }

    override fun ensure() {
        selectDistrictPosition = districtAdapter.selectPosition
        selectPiecePosition = pieceAdapter.selectPosition
        selectPlacePosition = placeAdapter.selectPosition
        if (selectPlacePosition != 0) {
            binding.tvDistrict.text = placeAdapter.getItem(selectPlacePosition).text
        } else if (selectPiecePosition != 0) {
            binding.tvDistrict.text = pieceAdapter.getItem(selectPiecePosition).text
        } else {
            binding.tvDistrict.text = districtAdapter.getItem(selectDistrictPosition).text
        }
        binding.tvDistrict.isSelected = true
        binding.ivDistrict.isSelected = true
    }

    override fun reset() {
        val selectDistrict = defaultDistrictPosition
        districtAdapter.selectPosition = selectDistrict
        districtAdapter.notifyDataSetChanged()

        val selectPiece =0
        val selectPlace =0
        pieceAdapter.selectPosition = selectPiece
        placeAdapter.selectPosition = selectPlace
        pieceAdapter.notifyDataSetChanged()
        placeAdapter.notifyDataSetChanged()

        binding.recyclerView2.visible = selectDistrict != 0
        binding.recyclerView3.visible = selectDistrict != 0 && selectPiece != 0

        scrollToRightPosition()
    }


    override fun close() {
        super.close()
        binding.recyclerView3.setPadding(0, 0, 0, 0)
        districtAdapter.selectPosition = selectDistrictPosition
        pieceAdapter.selectPosition = selectPiecePosition
        placeAdapter.selectPosition = selectPlacePosition
    }

    //---------------------自定义方法---------------------------
    private fun scrollToRightPosition() {
        if (districtAdapter.selectPosition >= 0) {
            binding.recyclerView1.scrollToPosition(districtAdapter.selectPosition)
        }
        if (pieceAdapter.selectPosition >= 0) {
            binding.recyclerView2.scrollToPosition(pieceAdapter.selectPosition)
        }
        if (placeAdapter.selectPosition >= 0) {
            binding.recyclerView3.scrollToPosition(placeAdapter.selectPosition)
        }
    }

    fun setDefaultDistrict(disrictStr: String) {
        districtAdapter.data.mapIndexed { index, downWinTextBean ->
            if (downWinTextBean.text == disrictStr) {
                selectDistrictPosition = index
                defaultDistrictPosition = index
                districtAdapter.selectPosition = index
                districtAdapter.notifyDataSetChanged()
                ensure()
            }
        }
    }
}